#coding=utf-8
#auth:beck shen
#date:2017-11-08


from pytw.core.tasklet import Tasklet 
from twisted.web import wsgi as _wsgi
from twisted.web import server


class _WSGIResponse(_wsgi._WSGIResponse):

    """
    使用tasklet运行
    """

    def start(self):
        """
        线程中运行tasklet
        """
        self.threadpool.callInThread(self._tasklet_start)
        
    def _tasklet_start(self):
        Tasklet.create(self.run)


class WSGIResource(_wsgi.WSGIResource):

    """
    改写render方法
    """

    def render(self, request):
        response = _WSGIResponse(
            self._reactor, self._threadpool, self._application, request)
        response.start()
        return server.NOT_DONE_YET
        
